package com.wuzhiming.istudy.controller;

import com.alibaba.fastjson.JSONObject;
import com.wuzhiming.istudy.pojo.Course;
import com.wuzhiming.istudy.utils.LogUtils;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: ArticleController
 * @Description:
 * @Author:zhimingw
 * @Version 1.0
 * @since 2021/7/14 16:25
 */
@Controller
public class CourseController extends BaseController{
    private Logger logger = LogUtils.getInstance(CourseController.class);

    
    /**
    * @Description: 轻删除【将课程移到回收站】
    * @Param: [id, from]
    * @return: java.lang.String
    * @Author: zhimingw
    * @Date: 2021/7/14
    */
    @GetMapping("/deleteLight")
    public String deleteLight(@RequestParam("id")Integer id, @RequestParam("from")String from){
        Course course = courseService.queryById(id);
        course.setStatus(2);
        if (courseService.update(course)) {
            logger.warn("【成功】将课程移入回收站，ID："+id);
        }else {
            logger.warn("【失败】服务器异常,将文章移入回收站");
        }
        return "redirect:/courses?type="+from;
    }

    /**
     * @Description: 强删除【永久删除】
     * @Param: [id, from]
     * @return: java.lang.String
     * @Author: zhimingw
     * @Date: 2021/7/16
     */
    @GetMapping("/deleteStrong")
    public String deleteStrong(@RequestParam("id") Integer id,@RequestParam("from") String from){
        if(courseService.deleteById(id)){
            logger.warn("【成功】将课程永久删除,ID: "+id);
        }else{
            logger.warn("【失败】服务器异常，将课程永久删除，ID："+id);
        }
        return "redirect:/courses?type="+from;
    }

    /**
     * @Description: 清空回收站
     * @Param: []
     * @return: java.lang.String
     * @Author: zhimingw
     * @Date: 2021/7/16
     */
    @GetMapping("/cleanTrash")
    public String cleanTrash(){
        if(courseService.deleteTrash()>0){
            logger.warn("【成功】清空回收站");
        }else{
            logger.warn("【失败】清空回收站");
        }
        return "redirect:/courses";
    }

    /**
     * @Description:
     * @Param: [title, content, tags, kind, introduce]
     * @return: java.lang.String
     * @Author: zhimingw
     * @Date: 2021/7/22
     */
    @PostMapping("/publish")
    @ResponseBody
    public String publish(String title,String introduce,String courseUrl,Integer courseSections,String courseTime,Integer courseTypeId,Integer courseStageId,Integer sort){
        Integer id = courseService.publish(title, introduce, courseUrl, courseSections, courseTime,courseTypeId,courseStageId,1,sort);
        if(id!=null&&id!=0){
            result.setCode(200);
            logger.warn("【成功】课程发布成功，ID："+id);
            result.setData(id);
        }else{
            result.setCode(500);
            logger.warn("【失败】课程发布失败，ID："+id);
        }
        return JSONObject.toJSONString(result);
    }

    /**
     * @Description: 更新课程
     * @Param: [id, title, content, tags, kind, introduce, status]
     * @return: java.lang.String
     * @Author: zhimingw
     * @Date: 2021/7/21
     */
    @PostMapping("/update")
    @ResponseBody
    public String update(Integer id,String title,String introduce,String courseUrl,Integer courseSections,String courseTime,Integer courseTypeId,Integer courseStageId,Integer status,Integer sort){
        if(courseService.update(id,title,introduce,courseUrl,courseSections,courseTime,courseTypeId,courseStageId,status,sort)){
            result.setCode(200);
            logger.warn("【成功】文章更新成功，ID："+id);
            result.setData(id);
        }else{
            result.setCode(500);
            logger.warn("【失败】文章更新，没有修改任何信息");
        }
        return JSONObject.toJSONString(result);
    }

    /**
     * @Description: 还原文章（放入草稿箱)
     * @Param: [id, from]
     * @return: java.lang.String
     * @Author: zhimingw
     * @Date: 2021/7/16
     */
    @GetMapping("/restore")
    public String restore(@RequestParam("id")Integer id,@RequestParam("from")String from){
        Course course = courseService.queryById(id);
        course.setStatus(0);
        if(courseService.update(course)){
            logger.warn("【成功】将课程还原到草稿箱");
        }else{
            logger.warn("【失败】将文章还原到草稿箱");
        }
        return "redirect:/courses?type="+from;
    }

    /**
     * @Description: 保存草稿
     * @Param: [title, content, tags, kind, introduce]
     * @return: java.lang.String
     * @Author: zhimingw
     * @Date: 2021/7/22
     */
    @PostMapping("/draftPublish")
    @ResponseBody
    public String draftPublish(String title,String introduce,String courseUrl,Integer courseSections,String courseTime,Integer courseTypeId,Integer courseStageId,Integer sort){
        Integer id = courseService.publish(title, introduce, courseUrl, courseSections, courseTime,courseTypeId,courseStageId,0,sort);
        if(id!=null&&id!=0){
            result.setCode(200);
            logger.warn("【成功】课程草稿保存成功，ID："+id);
            result.setData(id);
        }else{
            result.setCode(500);
            logger.warn("【失败】课程草稿保存失败，ID："+id);
        }
        return JSONObject.toJSONString(result);
    }
}   
